package pl.edu.mimuw.irs.rcp.view.navigation.handler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

import pl.edu.mimuw.irs.rcp.controller.editor.workflow.handler.AbstractSelectionHandler;
import pl.edu.mimuw.irs.rcp.persistence.descriptor.HistoryFileDescriptor;
import pl.edu.mimuw.irs.rcp.persistence.descriptor.HistoryFilesDescriptor;
import pl.edu.mimuw.irs.rcp.util.Logger;
import pl.edu.mimuw.irs.rcp.util.MessageCode;

public class DeleteHistoryHandler extends AbstractSelectionHandler {

	// *** Attributes

	// *** Methods

	@Override
	protected Object execute(ExecutionEvent event, Object[] selection)
			throws ExecutionException {
		
		for (Object object : selection) {
			if (!(object instanceof HistoryFileDescriptor)) continue;
			HistoryFileDescriptor descriptor = (HistoryFileDescriptor) object;
			HistoryFilesDescriptor parent = (HistoryFilesDescriptor) descriptor.getParent();
			
			try {
				parent.removeFile(descriptor.getFile());
			} catch (Exception e) {
				Logger.handle(MessageCode.HISTORY_SAVE_DESCRIPTOR_PROBLEM, e, descriptor.getFile());
			}
		}
		
		return null;
	}
}
